package plUpdate;

import MaintMenu.MaintMenu;

import javax.swing.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;

public class PLUpdate extends JApplet {

    private JTextField jtfdnkNumber = new JTextField(6);
    private JTextField jtfplNumber = new JTextField(9);
    private JButton jbtUpdate = new JButton("Update Partslink #");
    private boolean liUpd =false;
    private boolean bkUpd =false;
    private String dnkNumberLi;
    private String plNumberLi;
    private String dnkNumberBk;
    private String plNumberBk;
    private String lastDnkNum;
    private String lastPlNum;
    private JTextField jtlastDnkNum = new JTextField(6);
    private JTextField jtlastPlNum = new JTextField(9);





    // Statement for executing queries
//    private Statement stmt;
//    private Statement stmtbl;

    /** Initialize the applet */
    public void init() {
        // Initialize database connection and create a Statemet object
        //initializeDB();


        jbtUpdate.addActionListener(
                new ActionListener() {
                    public void actionPerformed(ActionEvent e) {
                        jbtShowGrade_actionPerformed(e);
                    }
                });

        JPanel jPanel1 = new JPanel();

        jPanel1.add(new JLabel("Dnk #"));
        jPanel1.add(jtfdnkNumber);
        jPanel1.add(new JLabel("PartsLink #"));
        jPanel1.add(jtfplNumber);
        jPanel1.add(jbtUpdate);
        jPanel1.add(new JLabel("Last Item Updated :"));
        jPanel1.add(jtlastDnkNum);
        jPanel1.add(jtlastPlNum);
        getRootPane().setDefaultButton(jbtUpdate);
        jPanel1.isFocusable();
        jPanel1.hasFocus();


        jtlastPlNum.setEditable(false);
        jtlastDnkNum.setEditable(false);









        add(jPanel1);

//        JOptionPane.showMessageDialog(null," PLEASE CLOSE THIS WINDOW WHEN YOU ARE FINISHED WITH \n PARTS LINK UPDATE");

    }

    private void initializeDB() {

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

            //Connection to Hicksville
            String dataSourceName = "dbLI";
            String dbURL = (new StringBuilder()).append("jdbc:odbc:").append(dataSourceName).toString();
            Connection connection = DriverManager.getConnection(dbURL, "", "");
            Statement stmt = connection.createStatement();


            dnkNumberLi = jtfdnkNumber.getText().toUpperCase();

            plNumberLi = jtfplNumber.getText().toUpperCase();

//            JOptionPane.showMessageDialog(null,"Updating Hicksville Inventory");

            String queryString = "update tblinventory SET partslink = '"+plNumberLi+"' WHERE [Part#]='"+dnkNumberLi+"'" ;

            String queryString1 = "select  partslink , [part#]" +
                    "from tblinventory " +
                    "where tblinventory.[Part#] = '" + dnkNumberLi+"'";
            stmt.executeUpdate(queryString);
            ResultSet rset = null;
            rset = stmt.executeQuery(queryString1);

            if (rset.next()) {

                String plNum = rset.getString(1);
                String dnkNum = rset.getString(2);


                // Display result in a dialog box
                connection.close();
                liUpd=true;
//                JOptionPane.showMessageDialog(null,"Update Hicksville Inventory Successfully\n\n"  +
//                        "DNK #:   " + dnkNum+ "           has been pdated with PartsLink #:               " + plNum + " " );


            } else {
                connection.close();
                liUpd=false;

                // Display result in a dialog box
                JLabel notFound = new JLabel("Hicksville DNK # Not found",JLabel.CENTER);
                JOptionPane.showMessageDialog(null, notFound,null,JOptionPane.ERROR_MESSAGE );

            } connection.close();



        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    private void initializeDBbl() {

        try {


            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");


            //Connection to Brooklyn
            String dataSourceNameBL = "dbBL";
            String dbURLBL = (new StringBuilder()).append("jdbc:odbc:").append(dataSourceNameBL).toString();
            Connection connectionBL = DriverManager.getConnection(dbURLBL, "", "");
            Statement stmtbl = connectionBL.createStatement();



            dnkNumberBk = jtfdnkNumber.getText().toUpperCase();

            plNumberBk = jtfplNumber.getText().toUpperCase();

//            JOptionPane.showMessageDialog(null,"Updating Brooklyn Inventory");

            String queryString = "update tblinventory SET partslink = '"+plNumberBk+"' WHERE [Part#]='"+dnkNumberBk+"'" ;

            String queryString1 = "select  partslink , [part#]" +
                    "from tblinventory " +
                    "where tblinventory.[Part#] = '" + dnkNumberBk+"'";
            stmtbl.executeUpdate(queryString);
            ResultSet rset = null;
            rset = stmtbl.executeQuery(queryString1);

            if (rset.next()) {
                String plNum = rset.getString(1);
                String dnkNum = rset.getString(2);

                connectionBL.close();
                bkUpd =true;
                // Display result in a dialog box
//                JOptionPane.showMessageDialog(null,"Update Brooklyn Inventory Successfully\n\n"  +
//                        "DNK #:   " + dnkNum+ "           has been pdated with PartsLink #:               " + plNum + " " );


            } else {
                // Display result in a dialog box
                connectionBL.close();
                bkUpd =false;
                JLabel notFound = new JLabel("Brooklyn DNK # Not found",JLabel.CENTER);
                JOptionPane.showMessageDialog(null, notFound,null,JOptionPane.ERROR_MESSAGE );

            }connectionBL.close();

        }
        catch (Exception ex) {
            ex.printStackTrace();
        }finally {

            if (liUpd && bkUpd){
                String liRes = "Complete";
                String bkRes = "Complete";
                JOptionPane.showMessageDialog(null,

                        "DNK #:   " + dnkNumberLi+ "           has been pdated with PartsLink #:               " + plNumberLi + "\n "+
                        "Update Hicksville Inventory Successfully\n\n"  +
                        "DNK #:   " + dnkNumberBk+ "           has been pdated with PartsLink #:               " + plNumberBk + "\n "+
                        "Update Brooklyn Inventory Successfully\n\n");
//                        "Update Hicksville Inventory : "+liRes+"\n"+"Update Brooklyn Inventory   : "+bkRes+"\n\n\n"+"Partslink update Completed");
            }else{
                String liRes="";
                String bkRes="";
                liRes = (liUpd)? "Complete" : "Failed";
                bkRes = (bkUpd)? "Complete" : "Failed";
                JOptionPane.showMessageDialog(null,"Update Hicksville Inventory : "+liRes+"\n"+"Update Brooklyn Inventory   : "+bkRes+"\n\n\n"+"Partslink Update Failed, Please verify DNK# and network connection.");
        }
//            JOptionPane.showMessageDialog(null,"Update Hicksville Inventory : "+liUpd+"\n"+"Update Brooklyn Inventory   : "+bkUpd+"\n\n\n"+updStat);
        }fieldCleanup();
    }

    private void fieldCleanup(){
        jtlastDnkNum.setText(dnkNumberLi);
        jtlastPlNum.setText(plNumberLi);
        jtfdnkNumber.setText("");
        jtfplNumber.setText("");


        jtfdnkNumber.requestFocusInWindow();

    }
    private void jbtShowGrade_actionPerformed(ActionEvent e) {
        initializeDB();
        initializeDBbl();

    }




    /** PLUpdate method */

}
